//
//  BottomBtnPartialView.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/4/19.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "AcceptBtnPartialView.h"
#import "JFBDrawLineHelper.h"
#import "JFBStaffDS.h"

@interface AcceptBtnPartialView() {
    NSUInteger _hasDefaultStaff;
}
@property (weak, nonatomic) IBOutlet UIButton *refuseBtn;
@property (weak, nonatomic) IBOutlet UIButton *changeStaffBtn;
@property (weak, nonatomic) IBOutlet UIButton *acceptBtn;
- (IBAction)refuseAction:(id)sender;
- (IBAction)acceptAction:(id)sender;
- (IBAction)changeStaffAction:(id)sender;
@end

@implementation AcceptBtnPartialView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = frame;
    }
    return self;
}

- (void)setOrder:(JFBOrderDS*)order{
    [self setBackgroundColor:PanelBgColorImportant];
    self.drawLineHelper = [[JFBDrawLineHelper alloc]init];
    
    CGRect refuseRect = CGRectMake(0, 0, self.bounds.size.width / 4, cDetailBottomHeight);
    self.refuseBtn.frame = refuseRect;
    [self.refuseBtn setBackgroundColor:[UIColor whiteColor]];
    
    CGRect acceptRect = CGRectMake(self.bounds.size.width / 4, 0, self.bounds.size.width / 2, cDetailBottomHeight);
    self.acceptBtn.frame = acceptRect;
    [self.acceptBtn setBackgroundColor:ButtonBgColorImportant];
    [self.acceptBtn.titleLabel removeFromSuperview];
    
    [JFBStaffDS getDefaultStaff:^(JFBStaffDS *staff, NSError *error) {
        if( staff != nil ) {
            _hasDefaultStaff = 1;
            UILabel *title = [[UILabel alloc] init];
            title.text = @"配送";
            title.font = [UIFont systemFontOfSize:TextFontSizeH2];
            title.textColor = [UIColor whiteColor];
            CGSize titleSize = [title.text sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH2]}];
            title.frame = CGRectMake(kOrderCellSpacing, (cDetailBottomHeight - titleSize.height) / 2, titleSize.width, titleSize.height);
            [self.acceptBtn addSubview:title];
            
            UILabel *name = [[UILabel alloc] init];
            NSString *n = [NSString stringWithFormat: @"%@", staff.name];
            name.text = n;
            name.font = [UIFont systemFontOfSize:TextFontSizeH3];
            name.textColor = [UIColor whiteColor];
            CGSize nameSize = [n sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
            
            UILabel *mobile = [[UILabel alloc] init];
            mobile.text = staff.telephone;
            mobile.font = [UIFont systemFontOfSize:TextFontSizeH3];
            mobile.textColor = [UIColor whiteColor];
            CGSize mobileSize = [staff.telephone sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
            
            CGFloat spacing = (cDetailBottomHeight - nameSize.height - mobileSize.height) / 2;
            CGFloat nameY = spacing;
            CGFloat nameX = title.frame.origin.x + titleSize.width + kOrderCellSpacing;
            
            name.frame = CGRectMake(nameX, nameY, nameSize.width, nameSize.height);
            [self.acceptBtn addSubview:name];
            mobile.frame = CGRectMake(nameX, nameY + nameSize.height, mobileSize.width, mobileSize.height);
            [self.acceptBtn addSubview:mobile];
        } else {
            _hasDefaultStaff = 0;
            UILabel *title = [[UILabel alloc] init];
            title.text = @"配送";
            title.font = [UIFont systemFontOfSize:TextFontSizeH2];
            title.textColor = [UIColor whiteColor];
            CGSize titleSize = [title.text sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH2]}];
            title.frame = CGRectMake((acceptRect.size.width - titleSize.width) / 2 + 1, (cDetailBottomHeight - titleSize.height) / 2, titleSize.width, titleSize.height);
            [self.acceptBtn addSubview:title];
        }
    }];
    
    CGRect changeRect = CGRectMake(refuseRect.size.width + acceptRect.size.width + 1, 0, self.bounds.size.width / 4 - 1, cDetailBottomHeight);
    self.changeStaffBtn.frame = changeRect;
    [self.changeStaffBtn setBackgroundColor:ButtonBgColorImportant];
    
    [self.drawLineHelper addverticalLine:refuseRect.size.width + acceptRect.size.width y:kOrderCellSpacing / 2 color:[UIColor whiteColor] height:cDetailBottomHeight-kOrderCellSpacing isDash:false];
    
    self.height = cDetailBottomHeight;
}

-( void )triggerNotification:(NSString*)event{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"orderDetailForAction" object:self userInfo:[NSDictionary dictionaryWithObject:event forKey:@"event"]];
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // 1.获取上下文环境
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    [self.drawLineHelper drawRect:context];
}

- (IBAction)refuseAction:(id)sender {
    NSLog(@"refuse");
    [self triggerNotification:@"refuse"];
}

- (IBAction)acceptAction:(id)sender {
    NSLog(@"accept");
    if( _hasDefaultStaff ) {
        [self triggerNotification:@"accept"];
    } else {
        [self triggerNotification:@"changeStaff"];
    }
}

- (IBAction)changeStaffAction:(id)sender {
    NSLog(@"change");
    [self triggerNotification:@"changeStaff"];
}
@end
